﻿

using ConsoleDemo.Application;
using ConsoleDemoDb;
using Microsoft.Extensions.DependencyInjection;
using PoemGame.ComputerAnswer.Shared;

var services = Utility.GetService();

var service = services.GetService<IConsoleDemoAppService>();
var computerAnswerFactory = services.GetService<IComputerAnswerFactory>();

Console.WriteLine("选择游戏类型1:对诗2:接龙3:飞花令");
var gametypeInput = Console.ReadLine();
var gametype = "";
switch (gametypeInput)
{
    case "1":
        gametype = "Duishi";
        break;
    case "2":
        gametype = "Jielong";
        break;
    default:
    case "3":
        gametype = "Feihualing";
        break;

}

Console.WriteLine("输入上下文");
var context = Console.ReadLine();

var createResult = await service.CreateGameAsync(new PoemGame.Domain.GameAggregate.GameType(gametype, ""), context);

while (!createResult.IsSuccess)
{
    Console.WriteLine(createResult.Message);
    Console.WriteLine("重新输入上下文");
    context = Console.ReadLine();
    createResult = await service.CreateGameAsync(new PoemGame.Domain.GameAggregate.GameType(gametype, ""), context);

}

var isFinish = false;
while (!isFinish)
{
    var currentPlayer = await service.GetCurrentUser(createResult.GameId);
    Console.WriteLine($"{currentPlayer}作答:");
    var answer = "";
    if (currentPlayer == "我自己")
    {
        answer = Console.ReadLine();
    }
    else
    {
        answer = ComputerAnswer(await service.GetLastPropertyAnswerAsync(createResult.GameId), gametype, context);
        Console.WriteLine(answer);
    }
    var playres = await service.PlayAsync(createResult.GameId, answer);
    Console.WriteLine(playres.Message);
    isFinish = playres.IsFinish;
}

string ComputerAnswer(string lastanswer, string gametype, string gamecontext)
{
    var answerservice = computerAnswerFactory.Create(gametype);
    return answerservice.Answer(gamecontext, lastanswer);
}
